package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.ReportService;
import com.sun.prism.shader.Solid_TextureYV12_AlphaTest_Loader;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import sun.util.calendar.BaseCalendar;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author zhangmeng
 * @description 检查组控制器
 * @date 2019/9/18
 **/
@RestController
@RequestMapping("/report")
@Slf4j
public class ReportController {

    @Autowired
    private ReportService reportService;

    /**
     * 添加
     *
     * @return
     */
    @GetMapping("/getMemberReport")
    public Result getMemberReport() {
        log.info("[会员折线图查询]");
        Map<String, Object> map = reportService.selectMemberCount();
        return new Result(true, MessageConst.ADD_CHECKGROUP_SUCCESS, map);
    }

    @GetMapping("/getSetmealReport")
    public Result getSetmealReport() {
        log.info("[套餐预约占比查询]");
        Map<String, Object> map = reportService.selectSetmealCount();
        return new Result(true, MessageConst.ADD_CHECKGROUP_SUCCESS, map);
    }

    @GetMapping("/getBusinessReportData")
    public Result getBusinessReportData() {
        log.info("[运营数据查询]");
        Map<String, Object> map = reportService.selectBuinessReport();
        return new Result(true, MessageConst.ADD_CHECKGROUP_SUCCESS, map);
    }

    @GetMapping("/exportBusinessReport")
    public Result exportBusinessReport(HttpServletResponse response) {
        log.info("[导出运营数据报表]开始");
        Map<String, Object> map = reportService.selectBuinessReport();
        String reportDate = (String) map.get("reportDate");
        Integer totalMember = (Integer) map.get("totalMember");
        Integer todayNewMember = (Integer) map.get("todayNewMember");
        Integer thisWeekNewMember = (Integer) map.get("thisWeekNewMember");
        Integer thisMonthNewMember = (Integer) map.get("thisMonthNewMember");

        Integer todayOrderNumber = (Integer) map.get("todayOrderNumber");
        Integer todayVisitsNumber = (Integer) map.get("todayVisitsNumber");
        Integer thisWeekOrderNumber = (Integer) map.get("thisWeekOrderNumber");
        Integer thisWeekVisitsNumber = (Integer) map.get("thisWeekVisitsNumber");
        Integer thisMonthOrderNumber = (Integer) map.get("thisMonthOrderNumber");
        Integer thisMonthVisitsNumber = (Integer) map.get("thisMonthVisitsNumber");

        List<Map<String, Object>> hotSetmealList =(List<Map<String, Object>>) map.get("hotSetmeal");
        Map<String, Object> map1 = hotSetmealList.get(0);
        String name1 = (String)map1.get("name");
        Integer setmeal_count1 = (Integer) map1.get("setmeal_count");
        Double proportion1 = (Double) map1.get("proportion");
        Map<String, Object> map2 = hotSetmealList.get(1);
        String name2 = (String)map2.get("name");
        Integer setmeal_count2 = (Integer) map2.get("setmeal_count");
        Double proportion2 = (Double) map2.get("proportion");


//D:\lihunan\java作业\healthteam\hm_cp_381_healthteam_09\health-code\src\main\resources\report_template.xlsx
        //读取模板构造workBook
        try (InputStream is = this.getClass().getClassLoader().getResourceAsStream("report_template.xlsx");
             Workbook workbook = new XSSFWorkbook(is);
             OutputStream os = response.getOutputStream()) {
            Sheet sheet = workbook.getSheetAt(0);
            //获取sheet中的第n行，第m个单元格，设置单元格的值,n m为正整数
            sheet.getRow(1).getCell(1).setCellValue(reportDate);
           if (sheet.getRow(3).getCell(3)==null){
               sheet.getRow(3).createCell(3).setCellValue(totalMember);
           }else {
               sheet.getRow(3).getCell(3) .setCellValue(totalMember);}

            if (sheet.getRow(3).getCell(1)==null){
                sheet.getRow(3).createCell(1).setCellValue(todayNewMember);
            }else {
                sheet.getRow(3).getCell(1) .setCellValue(todayNewMember);}

            if (sheet.getRow(4).getCell(1)==null){
                sheet.getRow(4).createCell(1).setCellValue(thisWeekNewMember);
            }else {
                sheet.getRow(4).getCell(1) .setCellValue(thisWeekNewMember);}

            if (sheet.getRow(4).getCell(3)==null){
                sheet.getRow(4).createCell(3).setCellValue(thisMonthNewMember);
            }else {
                sheet.getRow(4).getCell(3) .setCellValue(thisMonthNewMember);}

            if (sheet.getRow(6).getCell(1)==null){
                sheet.getRow(6).createCell(1).setCellValue(todayOrderNumber);
            }else {
                sheet.getRow(6).getCell(1) .setCellValue(todayOrderNumber);}

            if (sheet.getRow(6).getCell(3)==null){
                sheet.getRow(6).createCell(3).setCellValue(todayVisitsNumber);
            }else {
                sheet.getRow(6).getCell(3) .setCellValue(todayVisitsNumber);}

            if (sheet.getRow(7).getCell(1)==null){
                sheet.getRow(7).createCell(1).setCellValue(thisWeekOrderNumber);
            }else {
                sheet.getRow(7).getCell(1) .setCellValue(thisWeekOrderNumber);}

            if (sheet.getRow(7).getCell(3)==null){
                sheet.getRow(7).createCell(3).setCellValue(thisWeekVisitsNumber);
            }else {
                sheet.getRow(7).getCell(3) .setCellValue(thisWeekVisitsNumber);}

            if (sheet.getRow(8).getCell(1)==null){
                sheet.getRow(8).createCell(1).setCellValue(thisMonthOrderNumber);
            }else {
                sheet.getRow(8).getCell(1) .setCellValue(thisMonthOrderNumber);}

            if (sheet.getRow(8).getCell(3)==null){
                sheet.getRow(8).createCell(3).setCellValue(thisMonthVisitsNumber);
            }else {
                sheet.getRow(8).getCell(3) .setCellValue(thisMonthVisitsNumber);}

            if (sheet.getRow(11).getCell(0)==null){
                sheet.getRow(11).createCell(0).setCellValue(name1);
            }else {
                sheet.getRow(11).getCell(0) .setCellValue(name1);}
            if (sheet.getRow(11).getCell(1)==null){
                sheet.getRow(11).createCell(1).setCellValue(setmeal_count1);
            }else {
                sheet.getRow(11).getCell(1) .setCellValue(setmeal_count1);}
            if (sheet.getRow(11).getCell(2)==null){
                sheet.getRow(11).createCell(2).setCellValue(proportion1);
            }else {
                sheet.getRow(11).getCell(2) .setCellValue(proportion1);}

            if (sheet.getRow(12).getCell(0)==null){
                sheet.getRow(12).createCell(0).setCellValue(name2);
            }else {
                sheet.getRow(12).getCell(0) .setCellValue(name2);}
            if (sheet.getRow(12).getCell(1)==null){
                sheet.getRow(12).createCell(1).setCellValue(setmeal_count2);
            }else {
                sheet.getRow(12).getCell(1) .setCellValue(setmeal_count2);}
            if (sheet.getRow(12).getCell(2)==null){
                sheet.getRow(12).createCell(2).setCellValue(proportion2);
            }else {
                sheet.getRow(12).getCell(2) .setCellValue(proportion2);}
            //写入网络输出流
            //写入返回流
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("content-Disposition", "attachment;fileName=" + reportDate  + "_report.xlsx");

            workbook.write(os);
            return null;
        } catch (IOException e) {
            log.error("", e);
            return new Result(false, MessageConst.ACTION_FAIL);
        }
    }

}


